package com.yonyou.pmclouds.basecom.util;

import com.yonyou.pmclouds.basecom.conf.USpaceProperties;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.uspace.util.USpaceUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * 文件预览服务类
 *
 * @author yzx
 * @Date: 2018/6/27 14:44
 */
public class FilePreviewUtil {
    // 特定的key（请求需要的）
    public static final String TOKEN_KEY = "token";
    public static final String TIMESTAMP_KEY = "timestamp";
    public static final String V_KEY = "v";
    public static final String AGENT_ID_KEY = "agent_id";
    public static final String URL_KEY = "url";
    public static final String FILE_NAME_KEY = "file_name";
    public static final String CONETNT_LENGTH_KEY = "Conetnt-Length";
    // 用于后期回调验证key
    public static final String EFFECT_START_TIME = "effect_start_time";
    // 必要的数据
    private USpaceProperties uspaceProperties;

    public FilePreviewUtil(USpaceProperties uspaceProperties) {
        this.uspaceProperties = uspaceProperties;
    }

    public Object sendRequest(String fileRuleUrl, long fileConetntLength) throws BusinessException {
        // 请求参数准备
        Map<String, String> parameters = new HashMap<>();
        //parameters.put(URL_KEY,"http://api.idocv.com/data/doc/manual.docx");// 用于测试
        parameters.put(URL_KEY, fileRuleUrl);
        parameters.put(FILE_NAME_KEY, "测试");// 目前这个字段感觉没用，没有显示文件真实名字的地方
        parameters.put(TIMESTAMP_KEY, String.valueOf(System.currentTimeMillis()));
        parameters.put(V_KEY, uspaceProperties.getV());
        parameters.put(AGENT_ID_KEY, uspaceProperties.getAgentId());
        parameters.put(EFFECT_START_TIME, String.valueOf(System.currentTimeMillis()));
        parameters.put(TOKEN_KEY, uspaceProperties.getToken());

        String url = USpaceUtil.buildGetUrl(uspaceProperties.getFileview().getUri(),uspaceProperties,parameters);
        Map<String, String> headers = new HashMap<>();
        // 设置文件大小
        headers.put(CONETNT_LENGTH_KEY, String.valueOf(fileConetntLength));
        return HttpTookit.doGet(url, null, headers);
    }

}
